define(["jquery", "easy-admin"], function ($, ea) {

    var init = {
        table_elem      : '#currentTable',
        table_render_id : 'currentTableRenderId',
        index_url       : 'coupon.coupon/index',
        add_url         : 'coupon.coupon/add',
        edit_url        : 'coupon.coupon/edit',
        delete_url      : 'coupon.coupon/delete',
        export_url      : 'coupon.coupon/export',
        set_field       : 'coupon.coupon/setField'
    };
    var form    = layui.form,
        laydate = layui.laydate;
    return {
        index: function () {
            ea.table.render({
                init: init,
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: 'id', search: false},
                    {field: 'coupon_title', title: '优惠券名称', search: false},
                    {field: 'coupon_price', title: '优惠券面值', search: false},
                    {field: 'coupon_type', search: 'select', selectList: ["满减","无门槛"], title: '优惠券类型'},
                    {field: 'use_type', search: 'select', selectList: ["通用","技师","服务"], title: '使用类型'},
                    {field: 'use_min_price', title: '消费门槛', search: false},
                    {field: 'receive_type', search: 'select', selectList: {"1":"新人券","2":"系统发送","3":"会员券","4":"积分兑换"}, title: '领取优惠券类型'},
                    {field: 'is_permanent', search: 'false', selectList: ["限制","无限制"], title: '是否无限制'},
                    {field: 'release_number', title: '发布数量', search: false},
                    {field: 'surplus_number', title: '剩余数量', search: false},
                    {field: 'status', search: 'false', selectList: ["关闭","开启"], title: '状态', templet: ea.table.switch},
                    {field: 'create_time', title: '创建时间', search: false},
                    {fixed: 'right',width: 150, title: '操作', templet: ea.table.tool}
                ]],
                where: {"delete_time":0},
                toolbar:['refresh','add','delete']
            });

            ea.listen();
        },
        add: function () {
            //自定义格式
            laydate.render({
                elem: "#start_time"
                ,format: 'yyyy年MM月dd日'
                ,trigger:'click'
            });
            laydate.render({
                elem: "#end_time"
                ,format: 'yyyy年MM月dd日'
                ,trigger:'click'
            });
            $('.times').hide();
            var coupon_type = '<div class="layui-form-item use_min_price">';
            coupon_type += '<label class="layui-form-label">消费门槛</label>';
            coupon_type += '<div class="layui-input-block">';
            coupon_type += '<input type="number" name="use_min_price" class="layui-input" lay-verify="required" placeholder="请输入消费门槛" value="0.00">\n';
            coupon_type += '</div></div>';
            $('.use_type').before(coupon_type);
            form.on("radio(coupon_type)", function (data) {
                if (Number(data.value) === 0) {
                    $('.use_min_price').remove();
                    $('.use_type').before(coupon_type);
                } else {
                    $('.use_min_price').remove();
                }
            })
            var integral_string = '<div class="layui-form-item integral">';
            integral_string += '<label class="layui-form-label">兑换所需积分</label>';
            integral_string += '<div class="layui-input-block">';
            integral_string += '<input type="number" name="integral" class="layui-input" lay-verify="required" placeholder="请输入积分" value="0">';
            integral_string += '</div></div>';

            form.on("radio(receive_type)", function (data) {
                if (Number(data.value) === 4) {
                    $('.integral').remove();
                    $('.time_limit').before(integral_string);
                } else {
                    $('.integral').remove();
                }
            })
            form.on("radio(time_limit)", function (data) {
                if (Number(data.value) === 0) {
                    $('.times').show();
                } else {
                    $('.times').hide();
                }
            })
            // 发布数量
            var string = '<div class="layui-form-item release_number">';
            string += '<label class="layui-form-label">发布数量</label>';
            string += '<div class="layui-input-block">';
            string += '<input type="text" name="release_number" class="layui-input" placeholder="请输入发布数量" value="0">';
            string += '</div>';
            string += '</div>';

            form.on("radio(permanent)", function (data) {
                if (Number(data.value) === 0) {
                    $('.release_number').remove();
                    $(".permanent").after(string);
                } else {
                    $('.release_number').remove();
                }
            })

            ea.listen();
        },
        edit: function () {
            //自定义格式
            laydate.render({
                elem: "#start_time"
                ,format: 'yyyy年MM月dd日'
                ,trigger:'click'
            });
            laydate.render({
                elem: "#end_time"
                ,format: 'yyyy年MM月dd日'
                ,trigger:'click'
            });
            var coupon_type = '<div class="layui-form-item use_min_price">';
            coupon_type += '<label class="layui-form-label">消费门槛</label>';
            coupon_type += '<div class="layui-input-block">';
            coupon_type += '<input type="number" name="use_min_price" class="layui-input" lay-verify="required" placeholder="请输入消费门槛" value="' +use_min_price+'">\n';
            coupon_type += '</div></div>';
            var coupon_type_checked = $("input[name='coupon_type']:checked").val();
            if(Number(coupon_type_checked) === 0){
                $('.use_type').before(coupon_type);
            }
            form.on("radio(coupon_type)", function (data) {
                if (Number(data.value) === 0) {
                    $('.use_min_price').remove();
                    $('.use_type').before(coupon_type);
                } else {
                    $('.use_min_price').remove();
                }
            })

            var time_value = $("input[name='is_time_limit']:checked").val();

            if(Number(time_value) === 0){
                $('.times').show();
            } else {
                $('.times').hide();
            }
            var integral_value = $("input[name='receive_type']:checked").val();
            var integral_string = '<div class="layui-form-item integral">';
            integral_string += '<label class="layui-form-label">兑换所需积分</label>';
            integral_string += '<div class="layui-input-block">';
            integral_string += '<input type="number" name="integral" class="layui-input" lay-verify="required" placeholder="请输入积分" value="'+integral+'">';
            integral_string += '</div></div>';
            if(Number(integral_value) === 4){
                $('.time_limit').before(integral_string);
            }
            form.on("radio(receive_type)", function (data) {
                if (Number(data.value) === 4) {
                    $('.integral').remove();
                    $('.time_limit').before(integral_string);
                } else {
                    $('.integral').remove();
                }
            })
            form.on("radio(time_limit)", function (data) {
                if (Number(data.value) === 0) {
                    $('.times').show();
                } else {
                    $('.times').hide();
                }
            })
            // 发布数量
            var string = '<div class="layui-form-item release_number">';
            string += '<label class="layui-form-label">发布数量</label>';
            string += '<div class="layui-input-block">';
            string += '<input type="text" name="release_number" class="layui-input" placeholder="请输入发布数量" value="'+surplus_member+'">';
            string += '</div>';
            string += '</div>';
            var value = $("input[name='is_permanent']:checked").val()
            if(Number(value) === 0){
                $(".permanent").after(string);
            }

            form.on("radio(permanent)", function (data) {
                if (Number(data.value) === 0) {
                    $('.release_number').remove();
                    $(".permanent").after(string);
                } else {
                    $('.release_number').remove();
                }
            })

            ea.listen();
        }
    };
});